package com.servlet;

import com.bean.Info;
import com.bean.Login;
import com.dao.InfoDao;
import com.dao.LoginDao;
import com.util.DBHelper;

import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.UUID;


public class InfoServlet extends HttpServlet {

	private static final long serialVersionUID = 2913983657686219101L;
	
	public void insert(HttpServletRequest req, HttpServletResponse resp){

    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        DBHelper DB=new DBHelper();
        SqlSession session = null;
        //连接不上数据库
        try {
            session = DB.session();
        } catch (Exception e) {
            e.printStackTrace();
        }
       //接受前台传递的参数
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String name =request.getParameter("name");
        String sex =request.getParameter("sex");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        //System.out.println(name+sex+email+password);
        InfoDao infoDao = session.getMapper(InfoDao.class);
        //UUID
        String s = UUID.randomUUID().toString();
        //去掉“-”符号
        s=s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
        LoginDao loginDao = session.getMapper(LoginDao.class);
        loginDao.insert(new Login(s,name,password,"0"));
        infoDao.insert(new Info(s,sex,email,"2"));
        session.commit();
        session.close();
        response.sendRedirect("jsp/public/success.jsp");
    }
}
